package com.tensquare.user.controller;

import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.user.annotation.PreAuthorize;
import com.tensquare.user.exception.UserRegisteredException;
import com.tensquare.user.pojo.TbUser;
import com.tensquare.user.service.TbUserService;
import com.tensquare.user.vo.UserInfoResponse;
import com.tensquare.user.vo.UserVO;
import com.tensquare.util.DateUtils;
import com.tensquare.util.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author long
 * @since 2021-07-16 17:00:08
 */
@Slf4j
@RestController
@RequestMapping(value = "user")
public class TbUserController {
    @Autowired
    private TbUserService tbUserService;

    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    public Result findById(@PathVariable String userId) {
        UserInfoResponse userInfo = tbUserService.findById(userId);
        return new Result(true, "查询成功", StatusCode.OK, userInfo);
    }

    @RequestMapping(value = "info", method = RequestMethod.GET)
    public Result findUser(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        UserInfoResponse userInfo = tbUserService.findById(userId);
        log.info("用户信息出参->{}", userInfo.toString());
        return new Result(true, "查询成功", StatusCode.OK, userInfo);
    }

    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    public Result register(@Valid @RequestBody TbUser tbUser) throws UserRegisteredException {
        tbUserService.register(tbUser);
        return new Result(true, "注册成功", StatusCode.OK);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Result logout(HttpServletRequest request) throws Exception {
        tbUserService.logout(request.getHeader("userId"));
        return new Result(true, "退出成功", StatusCode.OK);
    }

    @PreAuthorize(hasPermission = "user:list")
    @RequestMapping(value = "/list/{offset}/{limit}", method = RequestMethod.POST)
    public Result findByPage(@PathVariable int offset, @PathVariable int limit, @RequestBody Map<String, Object> map) {
        String regDates = (String) map.get("regDates");
        Map<String, Object> columns = (Map<String, Object>) map.get("columns");
        String[] split = regDates.split("~");
        Date beginRegTime = null;
        Date endRegTime = null;
        if (!StringHelper.isEmpty(regDates)) {
            beginRegTime = DateUtils.parse(DateUtils.FormatType.type1, split[0]);
            endRegTime = DateUtils.parse(DateUtils.FormatType.type1, split[1]);
        }
        UserVO users = tbUserService.findByPage(offset, limit, columns, beginRegTime, endRegTime);
        log.info("用户列表出参->{}", users.toString());
        return new Result(true, "查询成功", StatusCode.OK, users);
    }

    @PreAuthorize(hasPermission = "user:del")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result delUser(@RequestBody Map<String, String> map) {
        Result result = tbUserService.delUser(map.get("ids"));
        log.info("删除用户出参->{}", result);
        return result;
    }

    @PreAuthorize(hasPermission = "user:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateUser(@Valid @RequestBody TbUser tbUser) {
        tbUserService.updateUser(tbUser);
        return new Result(true, "修改成功", StatusCode.OK);
    }

    @PreAuthorize(hasPermission = "user:update")
    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
    public Result restPassword(@RequestBody Map<String, String> map) {
        String id = map.get("id");
        if (StringHelper.isEmpty(id)) {
            return new Result(false, "id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        tbUserService.restPassword(id);
        return new Result(true, "修改成功", StatusCode.OK);
    }

}
